﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AgileLogistics.View.User
{
    /// <summary>
    /// AddUser.xaml 的交互逻辑
    /// </summary>
    public partial class AddUser : System.Windows.Window
    {
        ViewModel.UserViewModel vm;

        Model.DBContext dbContext = new Model.DBContext();
        Model.Repository<Model.Users> RepUser = new Model.Repository<Model.Users>();
        public AddUser()
        {
            InitializeComponent();

            vm = DataContext as ViewModel.UserViewModel;

            //
            vm.CurrUser = new Model.Users();
            vm.DepList = dbContext.DB.Queryable<Model.Depment>().ToList();


            vm.RoleList = new System.Collections.ObjectModel.ObservableCollection<Model.Roles>( dbContext.DB.Queryable<Model.Roles>().ToList());
        }

        private void btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btn_Save_Click(object sender, RoutedEventArgs e)
        {

            if (tb_id.Text=="")
            {
                Growl.SuccessGlobal("请输入编号");
                return;
            }
            if (tb_name.Text == "")
            {
                Growl.SuccessGlobal("请输入姓名");
                return;
            }
            if (tb_dep.Text == "")
            {
                Growl.SuccessGlobal("请输入部门");
                return;
            }
            if (tb_sex.Text == "")
            {
                Growl.SuccessGlobal("请输入性别");
                return;
            }
            if (tb_post.Text == "")
            {
                Growl.SuccessGlobal("请输入职务");
                return;
            }
            if (tb_phone.Text == "")
            {
                Growl.SuccessGlobal("请输入电话");
                return;
            }

            if (vm.CurrUser.Id=="")
            {
                Growl.SuccessGlobal("请输入编号");
            }
            else
            {
                var m = vm.UserList.Where(it => it.Id == vm.CurrUser.Id).Count();
                    
                if (m >0)
                {
                    Growl.SuccessGlobal("请重新输入");
                }
                else
                {
                    if (txt_psd.Password != txt_psdRe.Password)
                    {
                        Growl.SuccessGlobal("两次密码输入不一致！");
                        return;
                    }
                    else
                    {
                        vm.CurrUser.Password = txt_psd.Password;
                    }
                    dbContext.DB.Insertable<Model.Users>(vm.CurrUser).ExecuteCommand();

                    Growl.SuccessGlobal("保存成功！");
                    vm.UserList = new System.Collections.ObjectModel.ObservableCollection<Model.Users>(RepUser.GetList());
                    this.Close();
                }
            }
        }
    }
}
